﻿// ***********************************************************************
// Apache 2.0 License
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Models
// File      : AttachmentModel.cs
//
// ***********************************************************************
using ContractMeow.Api.Dtos.Attachment;
using ContractMeow.Api.Dtos.Extensions;
using ContractMeow.Models.Attachments;
using System;
using System.Text.RegularExpressions;

namespace ContractMeow.Models
{
	public class AttachmentModel : IAttachmentModel
	{
		private static readonly Regex EventsRegex = new Regex("^Events:");
		private static readonly Regex PollsRegex = new Regex("^Polls:");

		internal AttachmentModel()
		{
			Type = AttachmentType.Unknown;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="AttachmentModel" /> class.
		/// This will transform any Attachment from API into a generic attachment 
		/// (Particularly useful for YModule and Message Attachments which at the time of this writing
		/// are not yet supported)
		/// </summary>
		/// <param name="apiAttachment">The API attachment.</param>
		internal AttachmentModel(IAttachment apiAttachment)
		{
			Id = apiAttachment.Id;
			Name = apiAttachment.Name;
			var yModule = apiAttachment as IYModuleAttachment;
			if (yModule != null)
			{
				if (yModule.YModule.AppId == "events")
					Name = EventsRegex.Replace(Name, "Event:");
				if (yModule.YModule.AppId == "polls")
					Name = PollsRegex.Replace(Name, "Poll:");
			}
			Description = apiAttachment.Description;
			CreatedAt = apiAttachment.CreatedAt;
			GroupId = apiAttachment.GroupId;
			Url = apiAttachment.Url.ToUri();
			WebUrl = apiAttachment.WebUrl.ToUri();
			AttachmentType type;
			Type = Enum.TryParse(apiAttachment.Type, true, out type) ? type : AttachmentType.Unknown;
		}

		#region IAttachmentModel
		public Int64 Id { get; set; }

		public DateTimeOffset? CreatedAt { get; set; }

		public Int64? GroupId { get; set; }

		private String _name;
		public String Name
		{
			get { return _name; }
			set
			{
				_name = value;
			}
		}

		public virtual bool HasHostDomain
		{
			get { return false; }
		}

		public bool HasName
		{
			get { return !string.IsNullOrWhiteSpace(Name); }
		}

		private String _description;
		public String Description
		{
			get { return _description; }
			set
			{
				_description = value;
			}
		}

		public bool HasDescription
		{
			get { return !string.IsNullOrWhiteSpace(Description); }
		}

		private Int64 _size;
		public Int64 Size
		{
			get { return _size; }
			set
			{
				_size = value;
			}
		}

		private AttachmentType _type;
		public AttachmentType Type
		{
			get { return _type; }
			set
			{
				_type = value;
			}
		}

		private Uri _url;
		public Uri Url
		{
			get { return _url; }
			set
			{
				_url = value;
			}
		}

		private Uri _webUrl;
		public Uri WebUrl
		{
			get { return _webUrl; }
			set
			{
				_webUrl = value;
			}
		}
		#endregion

		/// <summary>
		/// Performs an implicit conversion from <see cref="AttachmentModel" /> to <see cref="Attachment" />.
		/// </summary>
		public static implicit operator Attachment(AttachmentModel attachment)
		{
			return new Attachment
				{
					Id = attachment.Id,
					CreatedAt = attachment.CreatedAt,
					GroupId = attachment.GroupId,
					Url = attachment.Url == null ? "" : attachment.Url.ToString(),
					WebUrl = attachment.WebUrl == null ? "" : attachment.WebUrl.ToString(),
				};
		}

		/// <summary>
		/// Performs an explicit conversion from <see cref="Attachment" /> to <see cref="AttachmentModel" />.
		/// </summary>
		public static explicit operator AttachmentModel(Attachment attachment)
		{
			return new AttachmentModel(attachment);
		}

		/// <summary>
		/// Froms the I attachment.
		/// </summary>
		public static AttachmentModel FromIAttachment(IAttachment attachment)
		{
			return new AttachmentModel(attachment);
		}
	}
}